// var js = jQuery.noConflict();
$(function () {

    //加载头部
    $("#tou").load("Header.html",function () {
        $(".leftNav").attr("class","leftNav none");

        $(".nav").hover(function () {
            $(".leftNav").attr("class","leftNav");
        },function () {
            $(".leftNav").attr("class","leftNav none");
        })
    });

//加载购物车详情页购物车商品的列表
    productList();



});


//加载购物车详情页购物车商品列表
function productList() {
    $.ajax({
        url:"easybuy/carprolist",
        success:function (param){
            var products = param.products;
            var prices = param.prices;
            $.each(products,function (index,product){
                $(".car_tab").children().append(
                    "           <tr data-id="+product.id+">\n" +
                    "            <td>\n" +
                    "            \t<div class=\"c_s_img\"><img src=\"http://47.104.137.246:9999/files/"+product.filename+"\" width=\"73\" height=\"73\" /></div>\n" +
                    "              <div style='text-align: center;line-height: 75px'><a >"  +product.name+"</a></div>"+
                    "            </td>\n" +
                    "            <td align=\"center\">"+product.description+"</td>\n" +
                    "            <td align=\"center\">\n" +
                    "            \t<div class=\"c_num\">\n" +
                    "                    <input type=\"button\" value=\"\" class=\"car_btn_1\" />\n" +
                    "                \t<input type=\"text\" value=\""+product.carcount+"\" name=\"\" class=\"car_ipt\" readonly/>  \n" +
                    "                    <input type=\"button\" value=\"\" onclick=\"addUpdate1(jq(this));\" class=\"car_btn_2\" />\n" +
                    "                </div>\n" +
                    "            </td>\n" +
                    "            <td align=\"center\" style=\"color:#ff4e00;\">￥<span>"+product.price+"</span></td>\n" +
                    "            <td align=\"center\">26R</td>\n" +
                    "            <td align=\"center\"><a onclick=\"ShowDiv('MyDiv','fade')\" class='del' data-count="+product.carcount+">删除</a>&nbsp; &nbsp;<a href=\"#\"onclick=\"ShowDiv('MyDiv1','fade1')\" class='collect'>移入收藏夹</a></td>\n" +
                    "          </tr>");
                if (index%2==0){
                    $(".car_tab").children("tr").eq(index).attr("class","car_tr");
                }
            });
            $(".car_tab").append("" +
                "<tr height=\"70\" style='float: none'>\n" +
                "          \t<td colspan=\"6\" style=\"font-family:'Microsoft YaHei'; border-bottom:0;\">\n" +
                "            <span class=\"fr\" id='prices'>商品总价：<b style=\"font-size:22px; color:#ff4e00;\">￥<span>"+prices+"</span></b></span>\n" +
                "            </td>\n" +
                "          </tr>\n" +
                "          <tr valign=\"top\" height=\"150\">\n" +
                "          \t<td colspan=\"6\" align=\"right\">\n" +
                "            \t<a href=\"#\" onclick=\"window.history.go(-1)\"><img src=\"images/buy1.gif\" /></a>&nbsp; &nbsp; <a href=\"BuyCar_Two.html\"><img src=\"images/buy2.gif\" /></a>\n" +
                "            </td>\n" +
                "          </tr>");

            productJiaJian();

            deleteProduct();

            collectProduct();
        }
    });
}

//购物车商品数量加减
function productJiaJian() {

    $(".car_btn_1").click(function () {
        var price = $(this).parent().parent().next().children("span").eq(0).text();
        var productId = $(this).parents("tr").eq(0).data("id");
        var count = $(this).next().val();
        if (count>1){
            $(this).next().val(count-1);
            $.ajax({
                url: "easybuy/caradd",
                data:{
                    productId:productId,
                    direction:"jian"
                },
                success:function () {
                    var prices =  $("#prices").find("span").eq(0).text();
                    prices = Number(prices) - Number(price);
                    car();
                    $("#prices").find("span").eq(0).text(prices);

                }
            });
        }

    });
    $(".car_btn_2").click(function () {
        var price = $(this).parent().parent().next().children("span").eq(0).text();
        var productId = $(this).parents("tr").eq(0).data("id");
        var count = $(this).prev().val();
        $(this).next().val(count+1);
            $.ajax({
            url: "easybuy/caradd",
            data:{
                productId:productId,
            },
            success:function () {
                var prices =  $("#prices").find("span").eq(0).text();
                prices = Number(prices) + Number(price);
                car();
                $("#prices").find("span").eq(0).text(prices);

            }
        });
    });
}

//购物车加载
function car(){
    $.ajax({
        url:"easybuy/carprolist",
        success:function (param){
            var products = param.products;
            var prices = param.prices;
            if (products!=""&&products!=null){
                var countsum = 0;
                $(".cars").empty();
                $(".prices").empty();
                $.each(products,function (index,product) {
                    $(".cars").append("" +
                        "                <li>\n" +
                        "                    <div class=\"img\"><a href=\"#\"><img src=\"http://47.104.137.246:9999/files/"+product.filename+"\" width=\"58\" height=\"58\" /></a></div>\n" +
                        "                    <div class=\"name\"><a href=\"#\">"+product.name+"</a></div>\n" +
                        "                    <div class=\"price\"><font color=\"#ff4e00\">￥"+product.price+"</font> X"+product.carcount+"</div>\n" +
                        "                </li>");
                    countsum = countsum + product.carcount;
                });
                $(".prices").append("" +
                    "<div class=\"price_sum\">共计&nbsp; <font color=\"#ff4e00\">￥</font><span>"+prices+"</span></div>\n" +
                    "            <div class=\"price_a\"><a href=\"BuyCar.html\">去购物车结算</a></div>");
                $(".car_t").children("span").html(products.length);
            }

        }
    })
}

//删除购物车中的商品
function deleteProduct() {
    $(".del").click(function () {
        var id = $(this).parent().parent().data("id");
        var count = $(this).data("count");
        $(".b_sure").click(function () {
            $.ajax({
                url:"easybuy/delprofromcar",
                data:{
                    productId:id,
                    carcount:count
                },
                success:function (result) {
                    if (result == "success"){
                        location.href = "BuyCar.html";
                    }
                }
            })
        });
    });
}

//收藏购物车中的商品
function collectProduct() {
    $(".collect").click(function () {
        var id = $(this).parent().parent().data("id");
            $.ajax({
                url:"easybuy/favoritesadd",
                data:{
                    productId:id
                }
        });
    });
}





function ShowDiv(show_div,bg_div){
    document.getElementById(show_div).style.display='block';
    document.getElementById(bg_div).style.display='block' ;
    var bgdiv = document.getElementById(bg_div);
    bgdiv.style.width = document.body.scrollWidth;
    // bgdiv.style.height = $(document).height();
    $("#"+bg_div).height($(document).height());
};

function CloseDiv(show_div,bg_div)
{
    document.getElementById(show_div).style.display='none';
    document.getElementById(bg_div).style.display='none';
};


